package com.zy.homework1128;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author Gong
 * @Description
 * @dATE 2021/11/28 9:36
 **/
public class TestStr {
    private List<String> list = new ArrayList();
    public static void main(String[] args) {
        TestStr testStr = new TestStr();
       
        String str = "Hello Hello Chou " +
                "Hello Hadoop RPC " +
                "Hello Wncud Chou " +
                "Hello Hadoop MapReduce " +
                "Hello Dick Gu ";
        String[] strAry = str.split(" ");//原数据
        for (int i = 0; i < strAry.length; i++) {
            testStr.addNum(strAry[i]);//统计数量
        }
        testStr.showList();
    }



    /**
     * @Author GongXL
     * @Description  统计数量
     * @Date 10:58 2021/11/28
     * @Param [java.lang.String]
     * @return java.util.List
     **/
    public List addNum(String str){
        boolean falg = false;//默认没有找到字符串
        for (int i = 0; i < list.size(); i++) {
            String temp = list.get(i);
            if(null != temp && temp.length() > 0){
                String[] ary = temp.split(",");
                String key = ary[0];
                String val = ary[1];
                if(key.equals(str)){
                    falg  = true;//找到元素了
                    int num = Integer.parseInt(val) + 1;
                    val = num + "";
                    list.set(i,key+","+val);
                }

            }
        }
        if(!falg){
            list.add(str+","+1);
        }
       return list;
    }
    /**
     * @Author GongXL
     * @Description  显示数据内容
     * @Date 11:04 2021/11/28
     * @Param []
     * @return void
     **/
    public  void showList(){
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }
}
